package com.admin.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.sms.v20210111.SmsClient;
import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description
 * @Autor 伍军
 * @Date 2021/12/29 10:28
 * @Version 1.0
 **/
@Component
public class SmsSend {

    @Autowired
    SmsClient smsClient;

    public HashMap<String,Object> send(String tel) throws TencentCloudSDKException {
        HashMap<String,Object> map = new HashMap<String,Object>();
        //创建短信发送请求对象
        SendSmsRequest req = new SendSmsRequest();
        //接收短信手机号
        String[] phone = {tel};
        req.setPhoneNumberSet(phone);
        //个人短信模板模板Id
        req.setTemplateId("短信模板模板Id");
        //个人短信发送SDK
        req.setSmsSdkAppId("短信发送SDK");
        //个人短信签名
        req.setSignName("短信签名");
        //短信验证码
        String[] p = {"2345"};
        req.setTemplateParamSet(p);
        //发送对象，并获取获取响应对象
        SendSmsResponse resp = smsClient.SendSms(req);
        //将响应结果转换成字符串格式
        String info = SendSmsResponse.toJsonString(resp);
        // 打印结果信息
        System.out.println(info);

        try {
            //将字符串格式转换成Map 接口对象,获取短信发送成功的提示
            ObjectMapper mapper = new ObjectMapper();
            Map<String,Object> m = mapper.readValue(info, Map.class);
            List<Map<String,Object>> list =( List<Map<String,Object>>) m.get("SendStatusSet");
            System.out.println(list.get(0).get("Code"));
            if(list.get(0).get("Code").equals("Ok")){
                System.out.println("发送成功");
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        return map;
    }
}